package com.blkj.framework.web.exception;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.blkj.common.core.bean.R;
import com.blkj.common.exception.base.BaseException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 北浪 2021/11/19
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    private static final Log log = LogFactory.get();

    @ExceptionHandler(BaseException.class)
    public R userException(BaseException e){
        log.error("用户模块异常：【{}】",e.getDefaultMessage());
        return R.defineError(e);
    }

    /**
     * 系统异常
     */
    @ExceptionHandler(Exception.class)
    public R handleException(Exception e, HttpServletRequest request)
    {
        log.error("系统异常：{}",e.getMessage());
        return R.error(e.getMessage());
    }
}
